package sw6.visualschedule;

import sw6.girafAppKit.GirafActivity;
import sw6.visualschedule.entities.ScheduleActivity;
import sw6.visualschedule.entities.Step;
import sw6.visualschedule.extendedViews.StepGallery;
import sw6.visualschedule.extendedViews.StepView;
import sw6.visualschedule.extendedViews.StepViewAdapter;
import sw6.visualschedule.persistence.ScheduleActivityRepo;
import tools.BundleHelper;
import tools.SingletonMediaPlayer;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * Provides the interface for the steps of an activity, note this is the list which is to be showed to the front-end user.
 * @author SW6E
 */

public class StepList extends GirafActivity {
    
    private Context mContext;
	private transient ScheduleActivityRepo mActivityDb;
	private transient ScheduleActivity mActivity;
	
	private transient StepGallery mGallery;
	
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.steplist);
        
        mContext = getApplicationContext();
        mActivityDb = new ScheduleActivityRepo(mContext);
        
        final Bundle extras = getIntent().getExtras();
        int activityId = extras != null ? extras.getInt(BundleHelper.ACTIVITY_ID) : -1;
        
        //Receives the activity
        if (activityId == -1) {
            Toast.makeText(mContext, "No activity found.", Toast.LENGTH_SHORT);
            finish();
        } else {
            mActivity = mActivityDb.getById(activityId);
        }
        
        //Finds the max size for the views in the adapter
        final Display display = getWindowManager().getDefaultDisplay();
        final int maxWidth = (int)(display.getWidth() * 0.9);
        final int maxHeight = (int)((display.getHeight() * 0.65 ));
        
        //Creates the adapter
        final StepViewAdapter adapter = new StepViewAdapter(mContext, mActivity,
	        new OnClickListener() {
				@Override
				public void onClick(final View view) {
					StepView stepview = (StepView)mGallery.getSelectedView();
					
					if (view == stepview.getDoneButton()) {
						setResult(RESULT_OK);
						StepList.this.finish();
					}
				}
			},
			new OnClickListener() {
				@Override
				public void onClick(final View view) {
					StepView stepview = (StepView)mGallery.getSelectedView();
					
					if (view == stepview.getDoneButton()) {
						stepview.setVisibility(View.INVISIBLE);
						mGallery.onKeyDown(StepGallery.NEXT_STEP, null);
						
						final Step nextStep = (Step) mGallery.getItemAtPosition(mGallery.getSelectedItemPosition() + 1);
						nextStep.onClick(view);
					}
				}
			},
			maxWidth,
			maxHeight
        );
        mGallery = (StepGallery) findViewById(R.id.stepview_gallery);
        mGallery.setAdapter(adapter);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	if (mGallery.getSelectedItemPosition() == 0) {
    		
    		Step step = (Step)mGallery.getItemAtPosition(mGallery.getSelectedItemPosition());
    		step.onClick(new View(getApplicationContext()));
    	}
    }
    
    
    @Override
    protected void onPause() {
        StepView child;
        
        for (int i = 0; i < mGallery.getChildCount(); i++) {
            child = (StepView)mGallery.getChildAt(i);
            
            if (child.getImage().getDrawable() != null) {
                child.getImage().getDrawable().setCallback(null);
            }
            
            child.destroyDrawingCache();
        }
        super.onPause();
    }
    
    @Override
    public void onBackPressed() {
    	SingletonMediaPlayer.getMediaPlayer().stop();
    	super.onBackPressed();
    }
}